<?php
namespace app\api\controller;

use think\Request;

class Room extends BaseController
{
    public function create()
    {
        $result = false;
        if (Request::instance()->isPost()) {
            $user_id = input('post.user_id');
            $build_id = input('post.build_id');
            $room_name = input('post.room_name');

            $room_model = model('Room');
            $room_model->data([
                'user_id' => $user_id,
                'build_id' => $build_id,
                'room_name' => $room_name
            ]);
            $result = $room_model->save();
        }

        return $result;
    }

    public function update()
    {
        $result = false;
        if (Request::instance()->isPost()) {
            $room_id = input('post.room_id');
            $build_id = input('post.build_id');
            $room_name = input('post.room_name');

            $room_model = model('Room');
            $result = $room_model->save([
                'build_id' => $build_id,
                'room_name' => $room_name,
            ],['room_id' => $room_id]);

        }

        return $result;
    }

    public function save()
    {
        $data = [];

        if (Request::instance()->isPost()) {
            $room_id = input('post.room_id');

            $result = isset($room_id) && $room_id ? $this->update() : $this->create();
            $data['status'] = $result !== false ? 200 : 400;
        }

        return json($this->dataReturn($data));
    }

    public function get()
    {
        $data = [];

        if (Request::instance()->isPost()) {
            $room_id = input('post.room_id');

            $room_model = model('Room');

            $data['status'] = 200;

            $field = [
                'r.*',
                'b.build_id',
                'b.build_address'
            ];

            $join = [
                ['__BUILD__ b', 'r.build_id = b.build_id']
            ];

            $where = [
                'room_id' => $room_id,
                'r.is_deleted' => '0'
            ];
            $data['room'] = $room_model->alias('r')->field($field)->join($join)->where($where)->find();

            $check_in_model = model('CheckIn');

            $check_in_field = [
                'check_in_id',
                'check_in_contact',
                'check_in_mobile',
                'check_in_date',
                'check_out_date',
                'check_in_status'
            ];

            $check_in_where = [
                'room_id' => $room_id,
                'is_deleted' => '0'
            ];

            $data['check_in'] = $check_in_model->field($check_in_field)->where($check_in_where)->find();
            if ($data['check_in']) {
                $data['check_in']['check_in_status'] = $data['check_in']['check_in_date'] <= strtotime(date('Y-m-d')) ? 1 : 0;
                $data['check_in']['check_in_date'] = date('Y-m-d', $data['check_in']['check_in_date']);
                if ($data['check_in']['check_out_date'])  $data['check_in'] = '';
            }
        }

        return json($this->dataReturn($data));
    }

    public function remove()
    {
        $data = [];

        if (Request::instance()->isPost()) {
            $room_id = input('post.room_id');

            $room_model = model('Room');
            $room_model->save([
                'is_deleted' => '1'
            ],['room_id' => $room_id]);

            $data['status'] = 200;
        }

        return json($this->dataReturn($data));
    }
}